package school30.ui.forms;

import school30.ui.Painter;
import school30.ui.events.Event;
import school30.ui.events.FocusEvent;
import school30.ui.events.KeyEvent;

public class Button extends Widget {

    protected Runnable action;
    protected int background = 0x0000ff;
    protected int foreground = 0xffffff;
    protected int borderColor = 0xffff00;
    protected boolean pressed;
    protected boolean toogle;
    protected boolean focused;
    protected String text = "Press me!";

    public Button() {
        focusable = true;
    }

    public void keyPressed(int code) {
        if (code == KeyEvent.KEY_NUM5 || code == KeyEvent.KEY_CENTER) {
            if (toogle) {
                pressed = !pressed;
            } else {
                pressed = true;
            }
            if (action != null) {
                action.run();
            }
            getForm().repaint();
        }
    }

    public void keyReleased(int code) {
        if (code == KeyEvent.KEY_NUM5 || code == KeyEvent.KEY_CENTER && !toogle) {
            pressed = false;
            getForm().repaint();
        }
    }

    public void handleEvent(Event evt) {
        if (evt instanceof KeyEvent) {
            KeyEvent e = (KeyEvent) evt;
            if (e.getType() == KeyEvent.TYPE_PRESSED) {
                keyPressed(e.getCode());
            } else if (e.getType() == KeyEvent.TYPE_RELEASED) {
                keyReleased(e.getCode());
            }
        } else if (evt instanceof FocusEvent) {
            FocusEvent e = (FocusEvent) evt;
            if (e.getType() == FocusEvent.TYPE_GAINED) {
                focusGained();
            } else if (e.getType() == FocusEvent.TYPE_LOSED) {
                focusLosed();
            }
        }
    }

    public void paint(Painter p) {
        p.setColor(background);
        p.fillRectangle(0, 0, position.getWidth(), position.getHeight());
        p.setColor(foreground);
        p.drawString(text, position.getWidth() / 2, position.getHeight() / 2, Painter.BASELINE | Painter.HCENTER);
        p.setColor(borderColor);
        if (pressed) {
            p.drawLine(0, position.getHeight() - 1, 0, 0);
            p.drawLine(position.getWidth() - 1, 0, 0, 0);
        } else {
            p.drawLine(0, position.getHeight() - 1, position.getWidth() - 1, position.getHeight() - 1);
            p.drawLine(position.getWidth() - 1, 0, position.getWidth() - 1, position.getHeight() - 1);
            if (!focused) {
                p.drawLine(1, position.getHeight() - 2, position.getWidth() - 2, position.getHeight() - 2);
                p.drawLine(position.getWidth() - 2, 1, position.getWidth() - 2, position.getHeight() - 2);
            }
        }
    }

    public void focusGained() {
        focused = true;
        getForm().repaintWidget(this);
    }

    public void focusLosed() {
        focused = false;
        getForm().repaintWidget(this);
    }

    public Runnable getAction() {
        return action;
    }

    public void setAction(Runnable action) {
        this.action = action;
    }

    public int getBackground() {
        return background;
    }

    public void setBackground(int background) {
        this.background = background;
    }

    public int getBorderColor() {
        return borderColor;
    }

    public void setBorderColor(int borderColor) {
        this.borderColor = borderColor;
    }

    public int getForeground() {
        return foreground;
    }

    public void setForeground(int foreground) {
        this.foreground = foreground;
    }

    public boolean isPressed() {
        return pressed;
    }

    public void setPressed(boolean pressed) {
        this.pressed = pressed;
    }

    public boolean isToogle() {
        return toogle;
    }

    public void setToogle(boolean toogle) {
        this.toogle = toogle;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}
